// 封装通用的逻辑函数
// 封装倒计时
import {ref, computed, onUnmounted} from 'vue'
import dayjs from 'dayjs'

export const useCountDown = () => {
        // 响应式的数据
        let timer = null
        const time = ref(0)
        const formatTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))
        // 开启倒计时的函数
        const start = (countDownTime) => {
                // 核心逻辑：每隔一秒开始减一
                time.value = countDownTime
                timer = setInterval(() => {
                        time.value--
                }, 1000)
        }
        // 组件销毁时清除定时器
        onUnmounted(() => {
                timer && clearTimeout(timer)
        })
        return {
                formatTime,
                start
        }
}
